package Serializable;
import java.io.*;

class Employee implements Serializable
{
    private final String name;
    private double salary;
    public Employee(String n,double s)
    {
        name = n;
        salary = s;
    }
    // 加薪资
    public void raiseSalary(double byPercent)
    {
        salary += salary*byPercent/100;
    }
    public String toString()
    {
        return getClass().getName()+"[name="+name+",salary="+salary+"]";
    }
}
class Manager extends Employee
{
    private Employee secretary;
    public Manager(String n,double s)
    {
        super(n,s);
        secretary = null;
    }
    // 设置秘书
    public void setSecretary(Employee s)
    {
        secretary = s;
    }
    public String toString()
    {
        return super.toString()+"[secretary="+secretary+"]";
    }
}
public class SerializableTest {
    public static void main(String[] args)
    {
        Employee harry = new Employee("Harry Hacker",50000);
        Manager manager = new Manager("Tony Tester",80000);
        manager.setSecretary(harry);
        Employee[] staff = new Employee[2];
        staff[0] = harry;
        staff[1] = manager;
        try
        {
            ObjectOutputStream out = new ObjectOutputStream(
                    new FileOutputStream("employee.dat"));
            out.writeObject(staff);
            out.close();
            ObjectInputStream in = new ObjectInputStream(
                    new FileInputStream("employee.dat"));
            Employee[] newStaff = (Employee[])in.readObject();
            in.close();
            newStaff[0].raiseSalary(10);
            for(var s:newStaff)
                System.out.println(s);
        }
        catch(IOException | ClassNotFoundException e)
        {
            System.out.println(e.getMessage());
        }
    }
}
